package com.todod.common.util;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import cn.dev33.satoken.stp.StpUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author gsh
 * @version 创建时间：2019年3月14日 下午2:53:01
 * 
 */

public class HTTPRequestUtil {

	private static Logger log = LoggerFactory.getLogger(HTTPRequestUtil.class);

	/**
	 * 
	 * @Title: doGet
	 * @Description:
	 * @author：gsh
	 * @date: 2019年3月14日
	 * @param url
	 * @return
	 * @return String
	 * @throws TokenOverException
	 */
	public static String doGet(String url, String parem, Map<String, String> headers) {
		HttpClient client = null;
		HttpResponse response = null;
		try {
			client = new DefaultHttpClient();
			// 发送get请求
			if (parem != null && !parem.equals("")) {
				url = url + "?" + parem;
			}

			HttpGet request = new HttpGet(url);

			// 设置请求头
			if (headers != null) {
				Set<String> keySet = headers.keySet();
				for (String headerKey : keySet) {
					request.setHeader(headerKey, headers.get(headerKey));
				}
			}

			response = client.execute(request);

			HttpEntity responseEntity = response.getEntity();
			String jsonString = EntityUtils.toString(responseEntity);

			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			/** 请求发送成功，并得到响应 **/
			if (state == HttpStatus.SC_OK) {
				return jsonString;
			} else {
				log.error("get请求异常,请求地址：" + url + ",响应码：" + state + ",响应内容：" + jsonString);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 
	 * @Title: doPost
	 * @Description: post请求
	 * @author：gsh
	 * @date: 2019年3月14日
	 * @param url
	 * @param params 参数username={username}&password={password}&grant_type=password
	 * @return
	 * @return String
	 */
	public static String doPost(String url, String params) {
		CloseableHttpResponse response = null;
		CloseableHttpClient httpclient = null;
		try {
			httpclient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);// 创建httpPost
			httpPost.setHeader("Accept", "application/json");
			httpPost.setHeader("Content-Type", "application/json");
			httpPost.setHeader("X-Token", StpUtil.getTokenValue());
			String charSet = "UTF-8";
			StringEntity entity = new StringEntity(params, charSet);
			httpPost.setEntity(entity);

			response = httpclient.execute(httpPost);
			HttpEntity responseEntity = response.getEntity();
			String jsonString = EntityUtils.toString(responseEntity);

			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				return jsonString;
			} else {
				log.error("post请求异常,请求地址：" + url + "?" + params + ",响应码：" + state + ",响应内容：" + jsonString);
			}
		} catch (ClientProtocolException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}

	public static String doPost(String url, String params, Map<String, String> headers) {
		CloseableHttpResponse response = null;
		CloseableHttpClient httpclient = null;
		try {
			httpclient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);// 创建httpPost
			httpPost.setHeader("Accept", "application/json");
			httpPost.setHeader("Content-Type", "application/json");

			// 设置请求头
			if (headers != null) {
				Set<String> keySet = headers.keySet();
				for (String headerKey : keySet) {
					httpPost.setHeader(headerKey, headers.get(headerKey));
				}
			}

			if (params != null) {
				String charSet = "UTF-8";
				StringEntity entity = new StringEntity(params, charSet);
				httpPost.setEntity(entity);
			}

			response = httpclient.execute(httpPost);
			HttpEntity responseEntity = response.getEntity();
			String jsonString = EntityUtils.toString(responseEntity);

			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				return jsonString;
			} else {
				log.error("post请求异常,请求地址：" + url + "?" + params + ",响应码：" + state + ",响应内容：" + jsonString);
			}
		} catch (ClientProtocolException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}
}
